package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"house_srv/house"
	"house_srv/internal/svc"
	"house_srv/models"
	"house_srv/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改个人信息
func (l *UserUpdateLogic) UserUpdate(in *house.UserUpdateRequest) (*house.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var houses models.House
	if err = houses.HouseList(); err != nil {
		logx.Error("用户有当前发布的房源,不允许修改")
		return nil, fmt.Errorf("用户有当前发布的房源,不允许修改")
	}
	users := models.User{
		Model: gorm.Model{
			ID: uint(in.Id),
		},
		Phone:    in.Phone,
		Password: pkg.Md5(in.Password),
	}

	models.DB.Debug().Updates(&users)
	return &house.UserUpdateResponse{
		Phone:    users.Phone,
		Password: pkg.Md5(users.Password),
		Id:       int64(users.ID),
	}, nil
}
